#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

#endregion

//============================================================================
//  File:    OptionForm.cs
//
//  Summary: Implements the Option Form of SQL2005 Service Manager
//
//----------------------------------------------------------------------------
//
// Copyright (C) sqldbatips.com.  All rights reserved.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//============================================================================

namespace SQL2005_Service_Manager
{
	//options that user can set for default service and polling inteval
	public partial class OptionForm : Form
	{

		#region Variables

		//defaults but should be overridden by property assignment
        private String defaultinstance = System.Environment.MachineName;
		private String defaultservice = SQLServiceList.SQL_SERVER;
		private int pollinginterval = 30;
        private SQLServiceList _sl;

		#endregion

		#region Properties

        // default instance
        public String DefaultInstance
        {
            get { return defaultinstance; }
            set { defaultinstance = value; }
        }

		//default service
		public String DefaultService
		{
			get { return defaultservice; }
			set { defaultservice = value; }
		}

		//service polling interval
		public int Pollinginterval
		{
			get { return pollinginterval; }
			set { pollinginterval = value; }
		}

        public SQLServiceList ServiceList
        {
            set { _sl = value; }
        }

		#endregion

		#region Constructor

		//Constructor
		public OptionForm()
		{
			InitializeComponent();
		}

		#endregion

		#region Events

		//form load
		private void OptionForm_Load(object sender, EventArgs e)
		{
			LoadOptions();
		}

		//cancel - don't save settings
		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		//save user settings
		private void btnOK_Click(object sender, EventArgs e)
		{
			SaveUserSettings();
			this.Close();
		}

		#endregion

		#region Methods

		//populate options dialog
		private void LoadOptions()
		{
            //default instance combobox
            cmbDefaultInstance.BeginUpdate();
            cmbDefaultInstance.DataSource = _sl.Instances();
            cmbDefaultInstance.DisplayMember = "InstanceName";
            cmbDefaultInstance.ValueMember = "InstanceName";

            if (!String.IsNullOrEmpty(defaultinstance))
            {
                int index = cmbDefaultInstance.FindStringExact(defaultinstance);
                if (index > -1)
                {
                    cmbDefaultInstance.SelectedIndex = index;
                }
                else
                {
                    cmbDefaultInstance.SelectedIndex = cmbDefaultInstance.FindStringExact(System.Environment.MachineName);
                }
            }
            cmbDefaultInstance.EndUpdate();

			//default service combobox
			cmbDefaultService.BeginUpdate();
			cmbDefaultService.Items.Add(SQLServiceList.ANALYSIS);
			cmbDefaultService.Items.Add(SQLServiceList.FTSEARCH);
			cmbDefaultService.Items.Add(SQLServiceList.MSDTC);
			cmbDefaultService.Items.Add(SQLServiceList.REPORT);
			cmbDefaultService.Items.Add(SQLServiceList.SQL_AGENT);
            cmbDefaultService.Items.Add(SQLServiceList.BROWSER);
			cmbDefaultService.Items.Add(SQLServiceList.SQL_SERVER);
            cmbDefaultService.Items.Add(SQLServiceList.SSIS);
            cmbDefaultService.Items.Add(SQLServiceList.SSIS2008);
			cmbDefaultService.Sorted = true;
			
			if (defaultservice != null)
			{
				int defaultindex = cmbDefaultService.FindStringExact(defaultservice);
				if (defaultindex > -1)
				{
					cmbDefaultService.SelectedIndex = defaultindex;
				}
				else
				{
					cmbDefaultService.SelectedIndex = cmbDefaultService.FindStringExact(SQLServiceList.SQL_SERVER);
				}
			}
			cmbDefaultService.EndUpdate();

			//polling interval
			udPolling.Value =(pollinginterval >= 30)? pollinginterval:30;

		}

		//Save user settings
		private void SaveUserSettings()
		{
			try
			{
                if (cmbDefaultInstance.SelectedIndex > -1)
                {
                    DataRowView drv = (DataRowView)cmbDefaultInstance.Items[cmbDefaultInstance.SelectedIndex];
                    defaultinstance = drv[0].ToString();
                }

				if (cmbDefaultService.SelectedIndex > -1)
				{
					defaultservice = cmbDefaultService.Items[cmbDefaultService.SelectedIndex].ToString();
				}

				pollinginterval = (int)udPolling.Value;

                Properties.Settings.Default.DefaultInstance = defaultinstance;
				Properties.Settings.Default.DefaultService = defaultservice;
				Properties.Settings.Default.PollingInterval = pollinginterval;
				Properties.Settings.Default.Save();
			}
			catch(Exception ex)
			{
				MainForm.ShowError(ex);
			}

		}

		#endregion

	}

}